package com.winit.openapi.gfs.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.gfs.spi.GfsEbayConfigService;
import com.winit.gfs.spi.command.GfsEbayConfigSearchCommand;
import com.winit.gfs.spi.command.GfsEbayConfigSubmitCommand;
import com.winit.gfs.spi.vo.GfsEbayConfigVo;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * ebay基础配置处理
 * 
 * @version 
 * <pre>
 * Author	Version		Date		Changes
 * kun.liu 	1.0  		2017-1-17 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value="/gfs/config")
public class GfsEbayConfigController extends BaseController{
	
    @Resource
    private GfsEbayConfigService gfsEbayConfigService;
	
    /**
     * 查询ebay基础配置
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryGfsEbayConfig")
    public String queryGfsEbayConfig(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
			// 调用接口查询ebay基础配置
			GfsEbayConfigSearchCommand command = new GfsEbayConfigSearchCommand();
			command.setcBpartnerId(Long.valueOf(CommandContext.getContext().getUserId()));
            if(json.containsKey("platform")){
                command.setPlatform(json.getString("platform"));
            }else{
                command.setPlatform("ebay");
            }
			GfsEbayConfigVo gfsEbayConfig = gfsEbayConfigService.queryGfsEbayConfig(command);
			
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("data", gfsEbayConfig);
			responseMsg.setData(data);
			
        }else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
    
    /**
     * 新增或更新ebay基础配置
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/submitGfsEbayConfig")
    public String submitGfsEbayConfig(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 校验参数
            GfsEbayConfigSubmitCommand command = buildGfsEbayConfig(json);
            GfsEbayConfigVo gfsEbayConfigVo = command.getGfsEbayConfigVo();
            
			GfsEbayConfigSearchCommand searchCommand = new GfsEbayConfigSearchCommand();
			searchCommand.setcBpartnerId(Long.valueOf(CommandContext.getContext().getUserId()));
			searchCommand.setPlatform(command.getGfsEbayConfigVo().getPlatform());
			GfsEbayConfigVo gfsEbayConfig = gfsEbayConfigService.queryGfsEbayConfig(searchCommand);
            if(null == gfsEbayConfig){
            	String noUpload = gfsEbayConfigVo.getNoUpload();
            	String uploadreplenish = gfsEbayConfigVo.getUploadReplenish();
            	if(StringUtils.isBlank(noUpload)){
            		gfsEbayConfigVo.setNoUpload(ApiConstant.IS_ACTIVE_Y);
            	}
            	if(StringUtils.isBlank(uploadreplenish)){
            		gfsEbayConfigVo.setUploadReplenish(ApiConstant.IS_ACTIVE_N);
            	}
            	if(StringUtils.isBlank(gfsEbayConfigVo.getOrderRule())){
                    gfsEbayConfigVo.setOrderRule("comb");
                }
            	
            	gfsEbayConfigService.saveGfsEbayConfig(command);
            	
            }else{
            	gfsEbayConfigService.updateGfsEbayConfig(command);
            	
            }
            
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
    
    /**
     * 组装数据
     * @param json
     * @return
     */
    private GfsEbayConfigSubmitCommand buildGfsEbayConfig(JSONObject json){
    	GfsEbayConfigSubmitCommand command = new GfsEbayConfigSubmitCommand();
    	GfsEbayConfigVo gfsEbayConfigVo = new GfsEbayConfigVo();
    	
    	if(json.containsKey("productRule")){
    		gfsEbayConfigVo.setProductRule(json.getString("productRule"));
    	}
    	if(json.containsKey("orderRule")){
    		gfsEbayConfigVo.setOrderRule(json.getString("orderRule"));
    	}
    	if(json.containsKey("noUpload")){
    		gfsEbayConfigVo.setNoUpload(json.getString("noUpload"));
    	}
    	if(json.containsKey("pruleValue")){
    		gfsEbayConfigVo.setPruleValue(json.getString("pruleValue"));
    	}
    	gfsEbayConfigVo.setcBpartnerId(Long.valueOf(CommandContext.getContext().getUserId()));
    	if(json.containsKey("uploadReplenish")){
    		gfsEbayConfigVo.setUploadReplenish(json.getString("uploadReplenish"));
    	}
    	if(json.containsKey("specifications")){
    		gfsEbayConfigVo.setSpecifications(json.getString("specifications"));
    	}
    	if(json.containsKey("fieldLengthType")){
    		gfsEbayConfigVo.setFieldLengthType(json.getString("fieldLengthType"));;
    	}
    	if(json.containsKey("escapeCharType")){
    		gfsEbayConfigVo.setEscapeCharType(json.getString("escapeCharType"));;
    	}
        if(json.containsKey("platform")){
            gfsEbayConfigVo.setPlatform(json.getString("platform"));
        }else{
            gfsEbayConfigVo.setPlatform("ebay");
        }
    	gfsEbayConfigVo.setIsActive("Y");
    	gfsEbayConfigVo.setUpdated(new Date());
    	gfsEbayConfigVo.setUpdatedby(Long.valueOf(CommandContext.getContext().getUserId()));
        gfsEbayConfigVo.setCreated(new Date());
    	gfsEbayConfigVo.setCreatedby(Long.valueOf(CommandContext.getContext().getUserId()));
    	gfsEbayConfigVo.setOrganizationId(Long.valueOf(CommandContext.getContext().getOrgId()));
    	command.setGfsEbayConfigVo(gfsEbayConfigVo);
    	return command;
    }
    
}
